<template>
  <view class="my-userinfo-container">
    <!-- 头像昵称区域 -->
    <view class="top-box">
      <button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
        <image class="avatar" :src="userInfo.avatarUrl"></image>
      </button>
      <view class="nickname">
        <text>修改昵称：</text>
        <input type="nickname" class="input" :value="userInfo.nickName" @blur="changeNikeName" />
      </view>
    </view>
    <!-- 面板的列表区域 -->
    <view class="panel-list">
      <!-- 第一个面板 -->
      <view class="panel">
        <!-- panel 的主体区域 -->
        <view class="panel-body">
          <!-- panel 的 item 项 -->
          <view class="panel-item">
            <text>8</text>
            <text>收藏的店铺</text>
          </view>
          <view class="panel-item">
            <text>14</text>
            <text>收藏的商品</text>
          </view>
          <view class="panel-item">
            <text>18</text>
            <text>关注的商品</text>
          </view>
          <view class="panel-item">
            <text>84</text>
            <text>足迹</text>
          </view>
        </view>
      </view>
      <!-- 第二个面板 -->
      <view class="panel">
        <!-- 面板的标题 -->
        <view class="panel-title">我的订单</view>
        <!-- 面板的主体 -->
        <view class="panel-body">
          <!-- 面板主体中的 item 项 -->
          <view class="panel-item">
            <image src="/static/my-icons/icon1.png" class="icon"></image>
            <text>待付款</text>
          </view>
          <view class="panel-item">
            <image src="/static/my-icons/icon2.png" class="icon"></image>
            <text>待收货</text>
          </view>
          <view class="panel-item">
            <image src="/static/my-icons/icon3.png" class="icon"></image>
            <text>退款/退货</text>
          </view>
          <view class="panel-item">
            <image src="/static/my-icons/icon4.png" class="icon"></image>
            <text>全部订单</text>
          </view>
        </view>
      </view>
      <!-- 第三个面板 -->
      <view class="panel">
        <view class="panel-list-item">
          <text>收货地址</text>
          <uni-icons type="arrowright" size="15"></uni-icons>
        </view>
        <view class="panel-list-item">
          <text>联系客服</text>
          <uni-icons type="arrowright" size="15"></uni-icons>
        </view>
        <view class="panel-list-item" @click="logout">
          <text>退出登录</text>
          <uni-icons type="arrowright" size="15"></uni-icons>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { mapGetters, mapActions } from 'vuex';
export default {
  name: 'my-userinfo',
  data() {
    return {};
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  methods: {
    ...mapActions('user', ['asyncUpdateUserInfo', 'asyncUpdateAddress', 'asyncUpdateToken']),
    // 更换用户头像
    onChooseAvatar(e) {
      const { avatarUrl } = e.detail;
      // 更新vuex中保存的用户信息的头像数据
      this.asyncUpdateUserInfo({
        ...this.userInfo,
        avatarUrl
      });
    },
    // 更新昵称
    changeNikeName(e) {
      const nickName = e.detail.value;
      // 更新vuex中保存的用户信息的昵称数据
      this.asyncUpdateUserInfo({
        ...this.userInfo,
        nickName
      });
    },
    // 退出登录
    async logout() {
      const [err, res] = await uni.showModal({
        title1: '提示',
        content: '确认退出登录吗？'
      });
      if (err === null && res.confirm === true) {
        // 用户确认了退出登录的操作
        // 需要清空 vuex 中的 userInfo、token 和 address
        await this.asyncUpdateUserInfo({});
        await this.asyncUpdateAddress({});
        await this.asyncUpdateToken('');
      }
    }
  }
};
</script>

<style lang="scss">
.my-userinfo-container {
  height: 100%;
  // 为整个组件的结构添加浅灰色的背景
  background-color: #f4f4f4;

  .top-box {
    height: 400rpx;
    background-color: #c00000;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    .avatar-wrapper {
      width: 90px;
      height: 90px;
      border-radius: 45px;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      .avatar {
        display: block;
        width: 90px;
        height: 90px;
      }
    }

    .nickname {
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      font-weight: bold;
      font-size: 16px;
      margin-top: 10px;
      .input {
        color: white;
      }
    }
  }
  .panel-list {
    padding: 0 10px;
    position: relative;
    top: -10px;

    .panel {
      background-color: white;
      border-radius: 3px;
      margin-bottom: 8px;

      .panel-title {
        line-height: 45px;
        padding-left: 10px;
        font-size: 15px;
        border-bottom: 1px solid #f4f4f4;
      }

      .panel-body {
        display: flex;
        justify-content: space-around;

        .panel-item {
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: space-around;
          font-size: 13px;
          padding: 10px 0;

          .icon {
            width: 35px;
            height: 35px;
          }
        }
      }
      .panel-list-item {
        height: 45px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 15px;
        padding: 0 10px;
      }
    }
  }
}
</style>
